<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="./src/Itcast.core.js"></script>
    <script src="./src/Itcast.dom.js"></script>
    <script>
        var arr = [],
            push = arr.push;

        Itcast.prependChild = function ( currentNode, parentNode ) {
            if ( parentNode.childNodes.length == 0 ) {
                parentNode.appendChild( currentNode );
            } else {
                parentNode.insertBefore( currentNode, parentNode.firstChild );
            }
        }
        Itcast.fn.prependTo = function ( selector ) {
            var iObj = this.constructor( selector ); // 统一变成伪数组了
            // this 是 伪数组, iObj 也是伪数组, 将 this 中的每一个元素, 加到 iObj 中的每一个元素里
            var tmp = [], tmpNode;
            for ( var i = 0; i < this.length; i++ ) {
                for ( var j = 0; j < iObj.length; j++ ) {
                    tmpNode = j == iObj.length - 1 ? this[ i ] : this[ i ].cloneNode( true );
                    tmp.push( tmpNode );

                    // iObj[ j ].appendChild( tmpNode );
                    Itcast.prependChild( tmpNode, iObj[ j ] );
                }
            }

            // dfg -> 元素上

            // 返回的是一个新的 jq 对象, 用 tmp 中的元素组成
            var tmpIobj = this.constructor();
            tmpIobj.prevObject = this;
            push.apply( tmpIobj, tmp );
            return tmpIobj;
        }
        Itcast.fn.prepend = function  ( selector ) {
            this.constructor( selector ).prependTo( this );
            return this;
        }
        
    </script>
    <style>
        div {
            border: 1px dashed red;
            height: 200px;
            margin: 10px 0;
        }
        div>p {
            border: 1px solid blue;
            margin: 10px 0;
        }
        .c {
            background-color: green;
        }
    </style>
</head>
</head>
<body>
    <div><p>ppp</p></div>
    <div><p>ppp</p></div>
    <div><p>ppp</p></div>
</body>
<script>
    I( '<p>pp1</p><p>pp2</p>' ).prependTo( 'div' ).each(function () {
        this.className = 'c';
    });
</script>
</html>